/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import BO.Iter.GiaoVienBO;
import BO.Iter.HocKyBO;
import BO.Iter.HocPhanBO;
import BO.Iter.MonHocBO;
import BO.Iter.SinhVienBO;
import TableModel.Giaovien;
import TableModel.Hocky;
import TableModel.Hocphan;
import TableModel.Monhoc;
import TableModel.Sinhvien;
import java.util.ArrayList;
import java.util.Collection;

import java.util.List;
import java.util.Map;

import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;



/**
 *
 * @author vt
 */
public class LopController  {
     private GiaoVienBO  gvBo;
     private HocKyBO     hkBo;
     private  MonHocBO   mhBo;
     private SinhVienBO  svBo;
     private HocPhanBO   hpBO;
     private Hocphan     lop;
     private String      idGiaovien;
     private String      backpage;
      private String      id_giaovien;
//---------------------------------------get set-------------------------------------------------------//
    public void setLop(Hocphan lop) {
        this.lop = lop;
    }
    public String getId_giaovien() {
        return id_giaovien;
    }

    public void setId_giaovien(String id_giaovien) {
        this.id_giaovien = id_giaovien;
    }

    public SinhVienBO getSvBo() {
        return svBo;
    }
    public void setSvBo(SinhVienBO svBo) {
        this.svBo = svBo;
    }
    public HocPhanBO getHpBO() {
        return hpBO;
    }
    public void setHpBO(HocPhanBO hpBO) {
        this.hpBO = hpBO;
    }
    
    public Hocphan getLop() {
        return lop;
    }
    public String getIdGiaovien() {
        return idGiaovien;
    }
    public void setIdGiaovien(String idGiaovien) {
        this.idGiaovien = idGiaovien;
    } 
    public GiaoVienBO getGvBo() {
        return gvBo;
    }
    public void setGvBo(GiaoVienBO gvBo) {
        this.gvBo = gvBo;
    }
    public HocKyBO getHkBo() {
        return hkBo;
    }
    public void setHkBo(HocKyBO hkBo) {
        this.hkBo = hkBo;
    }
    public MonHocBO getMhBo() {
        return mhBo;
    }
    public void setMhBo(MonHocBO mhBo) {
        this.mhBo = mhBo;
    }
   //-------------------------------------Listener Value in Form -------------------------------------------------- 
    public void valueChangeMethod(ValueChangeEvent  event){
       UIInput input = (UIInput) event.getComponent();
       input.getValue();
       System.out.println("Id giao vien :" + input.getValue());
   }
   //----------------------------------------------get Request Response --------------------------------------------------
    
    public String getRequestParam(){
        FacesContext context = FacesContext.getCurrentInstance(); 
       Map map = context.getExternalContext().getRequestParameterMap();
       String id = (String) map.get("id_hocphan");
       backpage = (String) map.get("backpage");
       if(backpage.equals("gvienedit")){
           id_giaovien =(String) map.get("id_giaovien");
       }
      
      lop = hpBO.findById(id);
       return id;
    }
    
 // ---------------------------------action -------------------------------------------------------------------------
    
    public String actionEdit(Hocphan lop){
      return "hpedit";
  }
     public String actionEdit(){
        return "hpedit";
    }
    public String actionNew(){
        return "hpadd";
    }
    public String  actionAdd(Hocphan bean){
        hpBO.add(bean);
        return "hpshow?faces-redirect=true";
    }
    
    public void delete(Hocphan malop){
        hpBO.delete(malop);
    }
    public String backPage(Hocphan a){
        hpBO.edit(a);
         FacesContext context = FacesContext.getCurrentInstance(); 
         if(backpage.equals("gvienedit")){
             context.getExternalContext().getRequestMap().put("id_giaovien", id_giaovien);
             System.out.println("id of GetRequest in Hoc Phan: " + id_giaovien);
         }
      return backpage;
    }
    
    
    
  //----------------------------------------------------------database-----------------------------------------------------
  public String layGiaoVien(String idhocphan){
      Giaovien a = gvBo.findGiaoVienById(idhocphan);
      if(a!= null){
          return a.getTengv();
      }else{
          return "";
      }
  }
  public String layHocKy(String idhocky){
      Hocky a = hkBo.findHocKyById(idhocky);
      if(a!= null){
          return a.getTenhocky();
      }else{
          return "";
      }
  }
  public String layMonHoc(String idhocky){
      Monhoc a = mhBo.findById(idhocky);
        System.out.println("Ten mon hoc: " + a.getTenmh());
      if(a!= null){
          System.out.println("Ten mon hoc: " + a.getTenmh());
          return a.getTenmh();
      }else{
          return "";
      }
  }
   public int countSinhVien(String malop){
     List<Sinhvien> list = svBo.getListSinhVien(malop);
      return list.size();
     
  }
 
  public Collection getAllGiaoVien(){
      ArrayList arr = new ArrayList();
      List<Giaovien> list = gvBo.listAllGiaoVien();
      for(Giaovien item : list){
           SelectItem a = new SelectItem(item.getMagv(),item.getTengv());
           arr.add(a);
      }
      return arr;
   
}
  public Collection getAllMonHoc(){
      ArrayList arr = new ArrayList();
      List<Monhoc> list = mhBo.listAllMonHoc();
      for(Monhoc item : list){
           SelectItem a = new SelectItem(item.getMamh(),item.getTenmh());
           arr.add(a);
      }
      return arr;
   
}
  public Collection getAllHocKy(){
      ArrayList arr = new ArrayList();
      List<Hocky> list = hkBo.listAllHocKy();
      for(Hocky item : list){
           SelectItem a = new SelectItem(item.getMahk(), item.getTenhocky() + " | " + item.getNgaybd().toString() + " | " +  item.getNgaykt().toString());
           arr.add(a);
      }
      return arr;
}
  public List<Hocphan> getAll() {
            return hpBO.listAll();
  }
   
    
  
}



